package Day2025_03_14;

public class LeetCode467 {
    public int findSubstringInWraproundString(String s) {
        int n = s.length();
        int[] dp = new int[n];
        for (int i = 0; i < n; i++) dp[i] = 1;
        for (int i = 1; i < n; i++) {
            char ch1 = s.charAt(i - 1), ch2 = s.charAt(i);
            if (ch1 == ch2 - 1 || (ch1 == 'z' && ch2 == 'a')) dp[i] += dp[i - 1];
        }
        int[] hash = new int[26];
        for (int i = 0; i < n; i++) {
            char ch = s.charAt(i);
            hash[ch - 'a'] = Math.max(dp[i], hash[ch - 'a']);
        }
        int ret = 0;
        for (int x : hash) ret += x;
        return ret;
    }
}
